package dao;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;

import util.RunMode;
import entity.Poem;
import entity.Row;
import entity.Word;

/**
 * This is the file that stores poems that player published. 
 */
public class PoemDAO {
	public static final String FILE_NAME = "published-poems.sav";
	public static final String HTML_FILE_NAME = "published-poems.html";
	
	/**
	 * Save a poem that player published.
	 */
	public void savePublishedPoems(ArrayList<Poem> poems, RunMode m, String fileName){
		if(m == RunMode.DEFAULT){
			try {
				FileOutputStream fout = new FileOutputStream(fileName);
				ObjectOutputStream oos = new ObjectOutputStream(fout);
				
				oos.writeObject(poems);
				
				oos.close();
				
				System.out.println("The Published Poems file was successfully saved.");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Save the poem in a HTML file with poems published before. 
	 */
	public void savePublishedPoemsHTML(ArrayList<Poem> poems, String fileName){
		@SuppressWarnings("unchecked")
		ArrayList<Poem> reversedPoems = (ArrayList<Poem>) poems.clone();
		Collections.reverse(reversedPoems);
		
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
			
			StringBuilder sb = new StringBuilder();

			sb.append("<html>\n");
				sb.append("<head>\n");
					sb.append("\t<title>CyberPoetry Slam - Published Poems - Team Pasiphae</title>\n");
					sb.append("\t<link rel='stylesheet' type='text/css' href='published-poems.css'>\n");
				sb.append("</head>\n");
			sb.append("<body>\n");
			
			sb.append("\t<div class='poemsContainer'>\n");
			sb.append("\t<h1>CyberPoetry Slam - Published Poems - Team Pasiphae</h1>\n");
			
			for(Poem p : reversedPoems){
				int negOffset = 0;
				
				for(Row r : p.getRows()){
					if(r.getOffsetX() < negOffset){
						negOffset = r.getOffsetX();
					}
				}

				sb.append("\t\t<div class='poem'>\n");
				
				sb.append("\t\t<div class='publishedOn'>" + new SimpleDateFormat("d MMM yyyy 'at' hh:mma").format(p.getPublishedOn()) + "</div>\n");
				
				for(Row r : p.getRows()){
					sb.append("\t\t\t<div class='row' style='margin-left: " + (r.getOffsetX() - negOffset) + "px;'>");
					
					for(Word w : r.getWords()){
						sb.append("<div class='word'>");
							sb.append(w.getValue());
						sb.append("</div>");
					}
					
					sb.append("\t\t\t</div>\n");
				}
				
				sb.append("\t\t</div>\n");
			}
			
			sb.append("\t</div>\n");

			sb.append("</body>\n");
			
			bw.write(sb.toString());
			
			bw.close();
			
			System.out.println("The Published Poems .html file was successfully saved.");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Get the published poems. 
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Poem> findPublishedPoems(String fileName){
		ArrayList<Poem> poems = new ArrayList<Poem>();
		
		try {
			FileInputStream fin = new FileInputStream(fileName);
			ObjectInputStream ois = new ObjectInputStream(fin);
			
			poems = (ArrayList<Poem>) ois.readObject();
			
			ois.close();
			
			System.out.println("Published Poems file successfully loaded.");
		} catch (FileNotFoundException e) {
			System.out.println("The Published Poems file wasn't found.");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		return poems;
	}
}
